package com.test.springboot.springboot3securitydemo1.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/9/29-15:21
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Setter
@Getter
public class AuthUser implements UserDetails {
  private Integer id;
  private String username;
  private String password;
  private Boolean enabled;
  private Boolean accountNonExpired;
  private Boolean accountNonLocked;
  private Boolean credentialsNonExpired;
  private List<Role> roles = new ArrayList<>();

  /**
   * @return
   */
  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    return roles.stream().map(r -> new SimpleGrantedAuthority(r.getName())).toList();
  }

  /**
   * @return
   */
  @Override
  public boolean isAccountNonExpired() {
    return accountNonExpired;
  }

  /**
   * @return
   */
  @Override
  public boolean isAccountNonLocked() {
    return accountNonLocked;
  }

  /**
   * @return
   */
  @Override
  public boolean isCredentialsNonExpired() {
    return credentialsNonExpired;
  }

  /**
   * @return
   */
  @Override
  public boolean isEnabled() {
    return enabled;
  }
}
